<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="二叉搜索树 BinarySearchTree 任意一个节点值都 大于 其左子树所有节点的值 任意一个节点的值都小于其右子树所有节点的值 它的左右子树也是一棵二叉搜索树 存数的元素必须具备比较性 不能为空   二叉搜索树-添加12345678910111213141516171819202122232425262728293031323334353637//	伪代码void add (E eleme">
<meta property="og:type" content="article">
<meta property="og:title" content="BinarySearchTree">
<meta property="og:url" content="http://yoursite.com/2019/10/12/SearchBinaryTree/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="二叉搜索树 BinarySearchTree 任意一个节点值都 大于 其左子树所有节点的值 任意一个节点的值都小于其右子树所有节点的值 它的左右子树也是一棵二叉搜索树 存数的元素必须具备比较性 不能为空   二叉搜索树-添加12345678910111213141516171819202122232425262728293031323334353637//	伪代码void add (E eleme">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-061916.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-063004.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-065007.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070038.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070445.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-021056.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-011012.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025001.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025022.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025405.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-030908.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065312.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065720.png">
<meta property="og:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-14-070327.png">
<meta property="og:updated_time" content="2019-10-15T02:32:58.828Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="BinarySearchTree">
<meta name="twitter:description" content="二叉搜索树 BinarySearchTree 任意一个节点值都 大于 其左子树所有节点的值 任意一个节点的值都小于其右子树所有节点的值 它的左右子树也是一棵二叉搜索树 存数的元素必须具备比较性 不能为空   二叉搜索树-添加12345678910111213141516171819202122232425262728293031323334353637//	伪代码void add (E eleme">
<meta name="twitter:image" content="http://server-name.test.upcdn.net/Algorithm/2019-10-12-061916.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/10/12/SearchBinaryTree/"/>





  <title>BinarySearchTree | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/12/SearchBinaryTree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">BinarySearchTree</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-12T14:09:47+08:00">
                2019-10-12
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/10/12/SearchBinaryTree/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/10/12/SearchBinaryTree/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="二叉搜索树-BinarySearchTree"><a href="#二叉搜索树-BinarySearchTree" class="headerlink" title="二叉搜索树 BinarySearchTree"></a>二叉搜索树 BinarySearchTree</h3><blockquote>
<p><strong>任意一个节点值都 大于 其左子树所有节点的值</strong></p>
<p><strong>任意一个节点的值都小于其右子树所有节点的值</strong></p>
<p><strong>它的左右子树也是一棵二叉搜索树</strong></p>
<p><strong>存数的元素必须具备比较性</strong></p>
<p><strong>不能为空</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-061916.png" alt=""></p>
<h3 id="二叉搜索树-添加"><a href="#二叉搜索树-添加" class="headerlink" title="二叉搜索树-添加"></a>二叉搜索树-添加</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	伪代码</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span> <span class="params">(E element)</span></span>&#123;</span><br><span class="line">	<span class="comment">//	判断element是否为空</span></span><br><span class="line">	<span class="keyword">if</span> (root == <span class="keyword">null</span>)&#123;</span><br><span class="line">		root = <span class="keyword">new</span> Node&lt;&gt;(element,父节点传<span class="keyword">null</span>);</span><br><span class="line">		size++;	<span class="comment">//	大小++ </span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//	到这里不是第一个节点,顺着根节点往下找添加合适的位置</span></span><br><span class="line">	Node&lt;E&gt; node = root;</span><br><span class="line">	<span class="comment">//	父节点</span></span><br><span class="line">	Node&lt;E&gt; parent = root;</span><br><span class="line">	<span class="comment">//	比较结果</span></span><br><span class="line">	<span class="keyword">int</span> cmp = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(node != <span class="keyword">null</span>)&#123;</span><br><span class="line">		cmp = compare(element,node.element);</span><br><span class="line">		<span class="comment">//	在这个位置保存父节点</span></span><br><span class="line">		parent = node;</span><br><span class="line">		<span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)&#123;					<span class="comment">//	如果存储的值大于 node,那么把node换成node.right	一直比较,直到right == null为止,那么在空的位置添加element</span></span><br><span class="line">			node = node.right</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span> (cmp &lt; <span class="number">0</span>)&#123;</span><br><span class="line">			node = node.left		<span class="comment">//	如果存储的值小于 node,那么把node换成node.left	一直比较,直到left == null为止,那么在空的位置添加element</span></span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				<span class="comment">//	覆盖原先的值</span></span><br><span class="line">				node.element = element;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//	循环结束之后 利用cmp判断在左还是右</span></span><br><span class="line">	<span class="keyword">if</span> (cmp &gt; <span class="number">0</span>)&#123;</span><br><span class="line">			parent.right = Node&lt;E&gt;(element,parent);	<span class="comment">//保存</span></span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			parent.left = Node&lt;E&gt;(element,parent);	<span class="comment">//保存</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	比较大小 返回 大于0 e1 &gt; e2  小于0 e1 &lt; e2 等于=0  e1=e2 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">compare</span><span class="params">(e1, e2)</span></span>&#123;<span class="keyword">return</span> el - e2;&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二叉搜索树-删除"><a href="#二叉搜索树-删除" class="headerlink" title="二叉搜索树-删除"></a>二叉搜索树-删除</h3><blockquote>
<p><strong>删除叶子节点,直接删除 node.parent.left or right = null, if node.parent == null, do root = null</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为1的跟节点root = child , child.parent = null</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为1的节点node,子节点是node.left或者子节点是node.right</strong></p>
<p><strong>用子节点替代node的位置</strong></p>
<p><strong>子节点的parent属性维护和parent的left或者right</strong></p>
</blockquote>
<blockquote>
<p><strong>删除度为2的节点node, 用node的前驱节点或者后继节点替换node</strong></p>
</blockquote>
<h3 id="前序遍历-Preorder-Traversal"><a href="#前序遍历-Preorder-Traversal" class="headerlink" title="前序遍历 (Preorder Traversal)"></a>前序遍历 (Preorder Traversal)</h3><blockquote>
<p><strong>访问顺序 根节点 , 前序遍历左子树,前序遍历右子树</strong></p>
<p><code>7</code>   , 4 , 2 , 1 , 3 , 5 ,        9 , 8 , 11 , 10 , 12</p>
</blockquote>
<h3 id=""><a href="#" class="headerlink" title=""></a><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-063004.png" alt=""></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)perorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">	  [self perorderTraversal:nodel.left];</span><br><span class="line">	  [self perorderTraversal:nodel.right];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="中序遍历-Inorder-Traversal"><a href="#中序遍历-Inorder-Traversal" class="headerlink" title="中序遍历 Inorder Traversal"></a>中序遍历 Inorder Traversal</h3><blockquote>
<p><strong>访问顺序 中序遍历左子树   根节点  中序遍历右子树</strong></p>
<p><strong>1  2  3  4  5    <code>7</code>     8  9  10  11  12  规律如果中序遍历的这个树是二叉搜索树,遍历出来的结果是升序,那么反过来就是降序</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-065007.png" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)inorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">	  [self inorderTraversal:nodel.left];</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">	  [self inorderTraversal:nodel.right];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="后序遍历-Post-order-Traversal"><a href="#后序遍历-Post-order-Traversal" class="headerlink" title="后序遍历 Post order Traversal"></a>后序遍历 Post order Traversal</h3><blockquote>
<p><strong>访问顺序 后序遍历左子树   后序遍历右子树  根节点,先访问左还是右 取决于自己,后序遍历的意思是根节点放在最后面就可以了</strong></p>
<p>1  3  2  5  4    8  10  12  11  9       <code>7</code></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070038.png" alt=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)postorderTraversal:(Node *)node&#123;</span><br><span class="line">		if (node == nil) return;</span><br><span class="line">	  [self postorderTraversal:nodel.left];</span><br><span class="line">	  [self postorderTraversal:nodel.right];</span><br><span class="line">		NSLog(@&quot;%@&quot;,node.element);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="层序遍历-Level-Order-Traversal"><a href="#层序遍历-Level-Order-Traversal" class="headerlink" title="层序遍历 Level Order Traversal"></a>层序遍历 Level Order Traversal</h3><blockquote>
<p><strong>访问顺序 从上到下 从左到右</strong></p>
<p><code>7</code>   4  9  2  5  8  11  1  3  10  12</p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-12-070445.png" alt=""></p>
<blockquote>
<p><strong>实现思路:  使用队列 先进先出FIFO</strong></p>
<p><strong>1. 将根节点入队</strong></p>
<p><strong>2.循环执行以下操作,直到队列为空</strong></p>
<p><strong>将队列的第一个元素出队,进行访问</strong></p>
<p><strong>将出队的左子节点入队</strong></p>
<p><strong>将出队的右子树入队</strong></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)levelOrderTraversal&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;</span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  		queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  		queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="计算二叉树的高度"><a href="#计算二叉树的高度" class="headerlink" title="计算二叉树的高度"></a>计算二叉树的高度</h3><p><strong>递归</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	查询子节点最高的节点的高度 + 1 就是字节的高度</span></span><br><span class="line">- (<span class="keyword">void</span>)height:(Node *)node&#123;</span><br><span class="line">	<span class="keyword">if</span> (node == nill) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span> + max(height(node.left),height(node.right));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>层序遍历</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)levelOrderTraversal&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;</span><br><span class="line">  	<span class="comment">//	树的高度</span></span><br><span class="line">		<span class="keyword">int</span> height = <span class="number">0</span>;	</span><br><span class="line">  	<span class="comment">//	在队列中存储的叶子总数</span></span><br><span class="line">  	<span class="keyword">int</span> levelSize = <span class="number">1</span>;	<span class="comment">//	根节点入队默认有一个</span></span><br><span class="line">  </span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  </span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">      </span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">      levelSize--;</span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">      </span><br><span class="line">      <span class="keyword">if</span> (levelSize == <span class="number">0</span>)&#123;</span><br><span class="line">        <span class="comment">//	levelSize == 0以为这这一层遍历完,要访问下一层</span></span><br><span class="line">        	levelSize = queue.size;</span><br><span class="line">					height++;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">	  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="判断一棵树是否为完全二叉树"><a href="#判断一棵树是否为完全二叉树" class="headerlink" title="判断一棵树是否为完全二叉树"></a>判断一棵树是否为完全二叉树</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-021056.png" alt=""></p>
<p><strong>层序遍历判断</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">boolean</span>)isComplete:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == <span class="keyword">null</span>) <span class="keyword">return</span>;</span><br><span class="line">		</span><br><span class="line">		Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  	<span class="keyword">boolean</span> leaf = <span class="keyword">false</span>;					<span class="comment">//	是否是叶子节点</span></span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (leaf &amp;&amp; (node.left || node.right))&#123;</span><br><span class="line">						<span class="keyword">return</span> <span class="keyword">false</span>;		<span class="comment">//	要求节点是叶子节点,但是不是那么不是完全二叉树</span></span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (root.left != <span class="keyword">null</span> &amp;&amp; root.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">            queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">              queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">         		&#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (root.left == <span class="keyword">null</span> &amp;&amp; root.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">          <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;		<span class="comment">//	后面遍历的节点都必须是叶子节点</span></span><br><span class="line">					leaf = <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> 	<span class="keyword">return</span> ture;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="翻转二叉树"><a href="#翻转二叉树" class="headerlink" title="翻转二叉树"></a>翻转二叉树</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-011012.png" alt=""></p>
<p><strong>利用前序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)perorderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span> root;</span><br><span class="line"></span><br><span class="line">			<span class="comment">//	每当遍历一个节点交换一个节点</span></span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			<span class="comment">//	这里交换之后遍历不会影响,只是先遍历左还是右</span></span><br><span class="line">	  	[self perorderTraversal:root.left];</span><br><span class="line">	  	[self perorderTraversal:root.right];</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>利用后序遍历翻转</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)perorderTraversal:(Node *)root&#123;</span><br><span class="line">		if (root == nil) return root;</span><br><span class="line"></span><br><span class="line">			//	这里交换之后遍历不会影响,只是先遍历左还是右</span><br><span class="line">	  	[self perorderTraversal:root.left];</span><br><span class="line">	  	[self perorderTraversal:root.right];</span><br><span class="line"></span><br><span class="line">			//	每当遍历一个节点交换一个节点</span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			</span><br><span class="line">  return root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>利用中序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)inorderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span> root;</span><br><span class="line"></span><br><span class="line">	  	[self inorderTraversal:root.left];</span><br><span class="line">	  	</span><br><span class="line">			<span class="comment">//	每当遍历一个节点交换一个节点</span></span><br><span class="line">			Node *tmp = root.left;</span><br><span class="line">			root.left = root.right;</span><br><span class="line">			root.right = root.tmp;</span><br><span class="line">			</span><br><span class="line">	  	[self inorderTraversal:root.left];</span><br><span class="line">	  	</span><br><span class="line">	  	</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>层序遍历翻转</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">- (Node *)levelOrderTraversal:(Node *)root&#123;</span><br><span class="line">		<span class="keyword">if</span> (root == nil) <span class="keyword">return</span>;  </span><br><span class="line">	  Queue&lt;Node&lt;E&gt;&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();	<span class="comment">//	创建队列</span></span><br><span class="line">	  queue.offer(root);						<span class="comment">//	将根节点入队</span></span><br><span class="line">  </span><br><span class="line">	  <span class="keyword">while</span>(!queue.isEmpty())&#123;</span><br><span class="line">      </span><br><span class="line">	  	Node&lt;E&gt; node = queue.poll()	<span class="comment">//出队访问</span></span><br><span class="line">	  	System.out.println(node.element);</span><br><span class="line">      </span><br><span class="line">      <span class="comment">//	交换</span></span><br><span class="line">      Node *tmp = node.left;</span><br><span class="line">			node.left = node.right;</span><br><span class="line">			node.right = node.tmp;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.left != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.left);		<span class="comment">//	将左子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">      </span><br><span class="line">	  	<span class="keyword">if</span> (node.right != <span class="keyword">null</span>)&#123;</span><br><span class="line">	  			queue.offer(node.right);	<span class="comment">//	将右子树入队</span></span><br><span class="line">	  	&#125;</span><br><span class="line">	  &#125;</span><br><span class="line">  <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="根据遍历结果还原二叉树"><a href="#根据遍历结果还原二叉树" class="headerlink" title="根据遍历结果还原二叉树"></a>根据遍历结果还原二叉树</h3><p><strong>以下结果可以保证还原出唯一的一棵二叉树</strong></p>
<p><strong>前序遍历 + 中序遍历</strong></p>
<p><strong>后序遍历 + 中序遍历</strong></p>
<p><strong>如果 只是给定前序遍历和后序遍历 那么必须是真二叉树才能还原出唯一的一棵二叉树</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025001.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025022.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-025405.png" alt=""></p>
<h3 id="前驱节点"><a href="#前驱节点" class="headerlink" title="前驱节点"></a>前驱节点</h3><blockquote>
<p><strong>中序遍历时的前一个节点</strong></p>
</blockquote>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-030908.png" alt=""></p>
<p><strong>8的前驱节点 是7,4的前驱节点是4,也就是node.left.right.right.right 直到 right == nil为止,度为1或者0</strong></p>
<p><strong>如果left == nil 那么一直往上找父节点,9的父节点是10(但9不是10的右节点),再往上找13(但10不是13的右节点),再往上找(13是8的右节点)8,那么8就是9的前驱节点</strong></p>
<p><strong>node.left == nil &amp;&amp; node.parent == nill 没有前驱节点</strong></p>
<h3 id="后继节点"><a href="#后继节点" class="headerlink" title="后继节点"></a>后继节点</h3><blockquote>
<p><strong>中序遍历后一个节点</strong></p>
</blockquote>
<p><strong>node.right.left.left.left 直到 left == nil 为止,度为1或者0</strong></p>
<h4 id="二叉搜索树删除"><a href="#二叉搜索树删除" class="headerlink" title="二叉搜索树删除"></a>二叉搜索树删除</h4><p><strong>比如要删除8,那么用8的前驱节点或者后继节点,替换掉8的值(不是删除8),再删除7</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	接口</span></span><br><span class="line">- (<span class="keyword">void</span>)removeElement:(E element)&#123;</span><br><span class="line">  	<span class="keyword">if</span> (element == <span class="keyword">null</span>) <span class="keyword">return</span>;</span><br><span class="line">  	removeNode(getNode(element));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	删除</span></span><br><span class="line">- (<span class="keyword">void</span>)removeNode:(Node *node)&#123;</span><br><span class="line">		size--;</span><br><span class="line">  	<span class="keyword">if</span> (node的度为<span class="number">2l</span>eft和right不为空)&#123;</span><br><span class="line">      Node *s = successor(node)	<span class="comment">//	找到后继节点</span></span><br><span class="line">      node.element = s.element	<span class="comment">//	替换值</span></span><br><span class="line">			node =  s;	???</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  <span class="comment">//	度为1的节点,走到这里必然是度为1的节点</span></span><br><span class="line">		Node *replacement = node.left != <span class="keyword">null</span> ? node.left : node.right;</span><br><span class="line">  	<span class="keyword">if</span> (replacement != <span class="keyword">null</span>)&#123;</span><br><span class="line">      	replacement.parent = node.parent;</span><br><span class="line">      <span class="comment">//	更改parent的left和right的值</span></span><br><span class="line">      <span class="keyword">if</span> (node.parent == <span class="keyword">null</span>)&#123;</span><br><span class="line">       root = replacement;  </span><br><span class="line">      &#125;<span class="keyword">else</span> <span class="keyword">if</span> (node == ndoe.parent.left)&#123;</span><br><span class="line">        node.parent.left = replacement;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">if</span> (node == ndoe.parent.right)&#123;</span><br><span class="line">        node.parent.right = replacement;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (node.parent == <span class="keyword">null</span>)&#123;	<span class="comment">//	node是叶子节点并且是根节点</span></span><br><span class="line">      root == <span class="keyword">null</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span>&#123;														<span class="comment">//	叶子节点</span></span><br><span class="line">      <span class="keyword">if</span> (node == node.parent.right)&#123;</span><br><span class="line">        node.parent.right == <span class="keyword">null</span>;</span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        node.parent.left == <span class="keyword">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">		&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//	拿到Node</span></span><br><span class="line">- (Node *)getNode:(E element)&#123;</span><br><span class="line">		Node *node = root;</span><br><span class="line">		<span class="keyword">while</span> (node != <span class="keyword">null</span>)&#123;</span><br><span class="line">			<span class="keyword">if</span> (node.element == element) <span class="keyword">return</span> node;</span><br><span class="line">			<span class="keyword">if</span> (node.element &lt; element)&#123;</span><br><span class="line">				node = node.right;</span><br><span class="line">			&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">				node = node.left;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="二叉搜索树复杂度分析"><a href="#二叉搜索树复杂度分析" class="headerlink" title="二叉搜索树复杂度分析"></a>二叉搜索树复杂度分析</h3><p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065312.png" alt=""></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-065720.png" alt=""></p>
<h3 id="改进二叉搜索树"><a href="#改进二叉搜索树" class="headerlink" title="改进二叉搜索树"></a>改进二叉搜索树</h3><p><strong>改进: 在节点的添加,删除操作之后,想办法让二叉搜索树恢复平衡(减少树的高度),左右子树的高度尽量接近</strong></p>
<p><img src="http://server-name.test.upcdn.net/Algorithm/2019-10-14-070327.png" alt=""></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/10/11/BinaryTree/" rel="next" title="BinaryTree">
                <i class="fa fa-chevron-left"></i> BinaryTree
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/10/14/AVLTree/" rel="prev" title="AVLTree">
                AVLTree <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉搜索树-BinarySearchTree"><span class="nav-number">1.</span> <span class="nav-text">二叉搜索树 BinarySearchTree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉搜索树-添加"><span class="nav-number">2.</span> <span class="nav-text">二叉搜索树-添加</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉搜索树-删除"><span class="nav-number">3.</span> <span class="nav-text">二叉搜索树-删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#前序遍历-Preorder-Traversal"><span class="nav-number">4.</span> <span class="nav-text">前序遍历 (Preorder Traversal)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#"><span class="nav-number">5.</span> <span class="nav-text"></span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#中序遍历-Inorder-Traversal"><span class="nav-number">6.</span> <span class="nav-text">中序遍历 Inorder Traversal</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后序遍历-Post-order-Traversal"><span class="nav-number">7.</span> <span class="nav-text">后序遍历 Post order Traversal</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#层序遍历-Level-Order-Traversal"><span class="nav-number">8.</span> <span class="nav-text">层序遍历 Level Order Traversal</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#计算二叉树的高度"><span class="nav-number">9.</span> <span class="nav-text">计算二叉树的高度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断一棵树是否为完全二叉树"><span class="nav-number">10.</span> <span class="nav-text">判断一棵树是否为完全二叉树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#翻转二叉树"><span class="nav-number">11.</span> <span class="nav-text">翻转二叉树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#根据遍历结果还原二叉树"><span class="nav-number">12.</span> <span class="nav-text">根据遍历结果还原二叉树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#前驱节点"><span class="nav-number">13.</span> <span class="nav-text">前驱节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后继节点"><span class="nav-number">14.</span> <span class="nav-text">后继节点</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#二叉搜索树删除"><span class="nav-number">14.1.</span> <span class="nav-text">二叉搜索树删除</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二叉搜索树复杂度分析"><span class="nav-number">15.</span> <span class="nav-text">二叉搜索树复杂度分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#改进二叉搜索树"><span class="nav-number">16.</span> <span class="nav-text">改进二叉搜索树</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
